package cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformmapping;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformmapping.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformmapping.PlatformMappingDO;
import cn.iocoder.yudao.module.crossborder.platform.service.platformmapping.PlatformMappingService;

@Tag(name = "管理后台 - 跨境电商数据映射配置")
@RestController
@RequestMapping("/crossborder/platform-mapping")
@Validated
public class PlatformMappingController {

    @Resource
    private PlatformMappingService platformMappingService;

    @PostMapping("/create")
    @Operation(summary = "创建跨境电商数据映射配置")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-mapping:create')")
    public CommonResult<Long> createPlatformMapping(@Valid @RequestBody PlatformMappingSaveReqVO createReqVO) {
        return success(platformMappingService.createPlatformMapping(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨境电商数据映射配置")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-mapping:update')")
    public CommonResult<Boolean> updatePlatformMapping(@Valid @RequestBody PlatformMappingSaveReqVO updateReqVO) {
        platformMappingService.updatePlatformMapping(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨境电商数据映射配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crossborder:platform-mapping:delete')")
    public CommonResult<Boolean> deletePlatformMapping(@RequestParam("id") Long id) {
        platformMappingService.deletePlatformMapping(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除跨境电商数据映射配置")
                @PreAuthorize("@ss.hasPermission('crossborder:platform-mapping:delete')")
    public CommonResult<Boolean> deletePlatformMappingList(@RequestParam("ids") List<Long> ids) {
        platformMappingService.deletePlatformMappingListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨境电商数据映射配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-mapping:query')")
    public CommonResult<PlatformMappingRespVO> getPlatformMapping(@RequestParam("id") Long id) {
        PlatformMappingDO platformMapping = platformMappingService.getPlatformMapping(id);
        return success(BeanUtils.toBean(platformMapping, PlatformMappingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨境电商数据映射配置分页")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-mapping:query')")
    public CommonResult<PageResult<PlatformMappingRespVO>> getPlatformMappingPage(@Valid PlatformMappingPageReqVO pageReqVO) {
        PageResult<PlatformMappingDO> pageResult = platformMappingService.getPlatformMappingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlatformMappingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨境电商数据映射配置 Excel")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-mapping:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlatformMappingExcel(@Valid PlatformMappingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlatformMappingDO> list = platformMappingService.getPlatformMappingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跨境电商数据映射配置.xls", "数据", PlatformMappingRespVO.class,
                        BeanUtils.toBean(list, PlatformMappingRespVO.class));
    }

}